package services;


import dao.Statistics_Repository;
import models.DatabaseExecutionContext;
import models.Statistics_F_Entity;
import play.db.jpa.JPAApi;

import javax.inject.Inject;
import java.util.List;
import java.util.concurrent.CompletionStage;

import static java.util.concurrent.CompletableFuture.supplyAsync;

public class Statistics_Repository_JPA implements Statistics_Repository {
    private final JPAApi jpaApi;
    private final DatabaseExecutionContext executionContext;

    @Inject
    public Statistics_Repository_JPA(JPAApi jpaApi, DatabaseExecutionContext executionContext) {
        this.jpaApi = jpaApi;
        this.executionContext = executionContext;
    }

    @Override
    public CompletionStage<List<Statistics_F_Entity>> listByProduct(String startTime, String endTime) {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                String strSql = "select * from f_StaticsByProduct('"+ startTime +"','"+endTime+"')";
                return entityManager.createNativeQuery(strSql, Statistics_F_Entity.class).getResultList();
            });
        }, executionContext);
    }

    @Override
    public CompletionStage<List<Statistics_F_Entity>> listByProductCompany(String startTime, String endTime, String productName) {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                String strSql = "select * from f_StaticsByProdComp('"+ startTime +"','"+endTime+"','"+productName+"')";
                return entityManager.createNativeQuery(strSql, Statistics_F_Entity.class).getResultList();
            });
        }, executionContext);
    }


    public CompletionStage<List<Statistics_F_Entity>> BDlistByCompany(String startTime, String endTime, String productName, String companyName) {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                String strSql = "select * from f_BDByComp('"+ startTime +"','"+endTime+"','"+productName+"','"+companyName+"')";
                return entityManager.createNativeQuery(strSql, Statistics_F_Entity.class).getResultList();
            });
        }, executionContext);
    }
}